package shiro.com.service.sys.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shiro.com.domain.sys.vo.PermissionListVo;
import shiro.com.domain.sys.vo.SysResoureVo;
import shiro.com.domain.sys.vo.SysResoureVo;
import shiro.com.service.sys.ISysResoureTestService;
import shiro.com.domain.sys.SysResoureTest;
import shiro.com.mapper.sys.SysResoureTestMapper;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-09
 */
@Service
public class SysResoureTestServiceImpl implements ISysResoureTestService
{
    @Autowired
    private SysResoureTestMapper sysResoureTestMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param sysReId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysResoureTest selectSysResoureTestBySysReId(Long sysReId)
    {
        return sysResoureTestMapper.selectSysResoureTestBySysReId(sysReId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysResoureTest 【请填写功能名称】
     * @return 【请填写功能名称】
     */

    @Override
    public List<SysResoureVo>  selectSysResoureTestList(SysResoureTest sysResoureTest)
    {
        //1查出所有分类
        List<SysResoureTest> sysResoureTestList = sysResoureTestMapper.selectSysResoureTestList(sysResoureTest);
        List<SysResoureVo> sysResoureVos = new ArrayList<>();
        sysResoureTestList.stream().filter(item->item.getSysReStatu()==1).map(item -> {
            SysResoureVo sysResoureVo = new SysResoureVo();
            BeanUtils.copyProperties(item, sysResoureVo);
            sysResoureVos.add(sysResoureVo);
            return sysResoureTestList;
        }).collect(Collectors.toList());
        //2组装成父子树形结构
        //找到所有的一级分类
        List<SysResoureVo> collect = sysResoureVos.stream().filter((e) -> {
            return e.getSysReParanterid() == 0;
        }).map((menu) -> {
            menu.setChildren(getChildrens(menu, sysResoureVos));

            return menu;
        }).sorted((menu1,menu2)->{

            return (menu1.getSysReSort()==null?0:menu1.getSysReSort())-(menu2.getSysReSort()==null?0:menu2.getSysReSort());
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<PermissionListVo>  selectSysResoureRoleList(String username)
    {
        //1查出所有分类
        List<SysResoureTest> sysResoureTests = sysResoureTestMapper.selectSysResoureLongByUserName(username);
        List<SysResoureVo> ListVo = sysResoureTests.stream().map(item -> {
            SysResoureVo SysResoureVo = new SysResoureVo();

            BeanUtils.copyProperties(item, SysResoureVo);

            return SysResoureVo;
        }).collect(Collectors.toList());

        List<PermissionListVo> collect = ListVo.stream().filter((e) -> {
            return e.getSysReParanterid() == 0;
        }).map((menu) -> {

            PermissionListVo permissionListVo = new PermissionListVo();
            permissionListVo.setComponent("");
            permissionListVo.setId(menu.getSysReId());
            permissionListVo.setIcon(menu.getSysReIcon());
            permissionListVo.setMetaname(menu.getSysReName());
            permissionListVo.setPath(menu.getSysReUrl());
            permissionListVo.setName(menu.getSysReName());
            permissionListVo.setChildren(getpermissionChildrens(permissionListVo, ListVo));
            return permissionListVo;
        }).collect(Collectors.toList());


        return collect;
    }


    private List<SysResoureTest> getChildrens(SysResoureVo root,List<SysResoureVo> all){
        List<SysResoureTest> children = all.stream().filter(e -> {
            return e.getSysReParanterid().equals(root.getSysReId());
        }).map(e1 -> {
            e1.setChildren(getChildrens(e1,all));
            return e1;
        }).sorted((menu1,menu2)->{
            return (menu1.getSysReSort()==null?0:menu1.getSysReSort())-(menu2.getSysReSort()==null?0:menu2.getSysReSort());
        })  .collect(Collectors.toList());
        return  children;
    }
    private List<PermissionListVo> getpermissionChildrens(PermissionListVo root,List<SysResoureVo> all){
        List<PermissionListVo> children = all.stream().filter(e -> {
            return e.getSysReParanterid().equals(root.getId());
        }).map(e1 -> {
            PermissionListVo permissionListVo = new PermissionListVo();
            permissionListVo.setComponent(e1.getSysReUrl() );
            permissionListVo.setId(e1.getSysReId());
            permissionListVo.setIcon(e1.getSysReIcon());
            permissionListVo.setMetaname(e1.getSysReName());
            permissionListVo.setPath(e1.getSysReUrl().split("/")[1]);
            permissionListVo.setName(e1.getSysReName());
            e1.setChildren(getChildrens(e1, all));
            return permissionListVo;
        }).collect(Collectors.toList());
        return  children;
    }


    @Override
    public List<SysResoureTest> selectSysResoureTestListAll() {
        return sysResoureTestMapper.selectSysResoureTestListAll();
    }



    /**
     * 新增【请填写功能名称】
     *
     * @param sysResoureTest 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysResoureTest(SysResoureTest sysResoureTest)
    {
        return sysResoureTestMapper.insertSysResoureTest(sysResoureTest);
    }

    @Override
    public int selectbtnList(String btnName)
    {
        return sysResoureTestMapper.selectbtnList(btnName);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysResoureTest 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysResoureTest(SysResoureTest sysResoureTest)
    {
        return sysResoureTestMapper.updateSysResoureTest(sysResoureTest);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param sysReIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysResoureTestBySysReIds(Long[] sysReIds)
    {
        return sysResoureTestMapper.deleteSysResoureTestBySysReIds(sysReIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param sysReId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysResoureTestBySysReId(Long sysReId)
    {
        return sysResoureTestMapper.deleteSysResoureTestBySysReId(sysReId);
    }
    @Override
    public Set<String> selectSysResoureTestByUserName(String userName){
        return sysResoureTestMapper.selectSysResoureTestByUserName(userName);
    }

}
